package serviceresource

import (
	"context"
	"github.com/aberic/gnomon"
	"github.com/pkg/errors"
	"google.golang.org/grpc"
	"gykjgit.dccnet.com.cn/chain/proto/resource"
)

// Resources 获取服务资源信息
func Resources(url string) (*resource.ResourceBaaS, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := resource.NewResourceClient(conn)
		// 客户端向grpc服务端发起请求
		return cli.Info(context.Background(), &resource.Request{})
	})
	if nil == err {
		if response, ok := resp.(*resource.ResourceBaaS); ok {
			return response, nil
		}
		return nil, errors.New("resources failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "resources failed")
}
